/*
 * Copyright 2018 - YZTC
 * 
 * YZTC
 * 本公司保留所有下述内容的权利。
 * 本内容为保密信息，仅限本公司内部使用。
 * 非经本公司书面许可，任何人不得外泄或用于其他目的。
 */
package com.wxmp.core.util;
import java.sql.Timestamp;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;
import java.util.Locale;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
public class UtilDateTime {
    private final static Logger log = LoggerFactory.getLogger(UtilDateTime.class);  
    public static final String datePartition = "-";
    public static String nowDateTimeString() {
        SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        return df.format(new Date());
    }
    public static String nowDateString(String formater) {
        SimpleDateFormat df = new SimpleDateFormat(formater);
        return df.format(new Date());
    }
    public static String nowDateString() {
        SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");
        return df.format(new Date());
    }
    public static Date formatString (String str)
    { 
        if (str == null)
        {
            return null;
        } 
        if(str.length()==10){
            str = str.trim()+" 00:00:00";
        }
        if(str.length()==16){
            str = str.trim()+":00";
        }
        SimpleDateFormat sdf = new SimpleDateFormat ("yyyy-MM-dd HH:mm:ss"); 
        Date date = null;
        try
        {
            date = sdf.parse (str);
        }
        catch (Exception e)
        {
        }
        return date;
    }
    public static String formatCNYear(Date d){
        if(d==null){
            return null;
        }
        SimpleDateFormat df = new SimpleDateFormat("yyyy年");
        return df.format(d);
    } 
    public static String formatCNMonth(Date d){
        if(d==null){
            return null;
        }
        SimpleDateFormat df = new SimpleDateFormat("yyyy年MM月");
        return df.format(d);
    } 
    public static String formatCNDate(Date d){
        if(d==null){
            return null;
        }
        SimpleDateFormat df = new SimpleDateFormat("yyyy年MM月dd日");
        return df.format(d);
    } 
    public static String formatCNTime(Date d){
        if(d==null){
            return null;
        }
        SimpleDateFormat df = new SimpleDateFormat("yyyy年MM月dd日 HH时mm分ss秒");
        return df.format(d);
    } 
    public static String formatDate(Date d){ 
        return formatDate(d,"yyyy-MM-dd");
    }
    public static String formatDate(Date d, String format){
        if(d==null){
            return null;
        }
        SimpleDateFormat df = new SimpleDateFormat(format);
        return df.format(d);
    }
    public static String formatDateTime(Date d){ 
        return formatDate(d,"yyyy-MM-dd HH:mm:ss");
    }
    public static java.sql.Timestamp nowTimestamp() {
        return new java.sql.Timestamp(System.currentTimeMillis());
    } 
    public static java.util.Date nowDate() {
        return new java.util.Date();
    }
    public static java.util.Date nowSqlDate() {
        return new java.sql.Date(nowDate().getTime());
    }
    public static java.sql.Timestamp getDayStart(java.sql.Timestamp stamp) {
        return getDayStart(stamp, 0);
    } 
    public static java.sql.Timestamp getDayStart(java.sql.Timestamp stamp, int daysLater) {
        Calendar tempCal = Calendar.getInstance();
        tempCal.setTime(new java.util.Date(stamp.getTime()));
        tempCal.set(
            tempCal.get(Calendar.YEAR),
            tempCal.get(Calendar.MONTH),
            tempCal.get(Calendar.DAY_OF_MONTH),
            0,
            0,
            0);
        tempCal.add(Calendar.DAY_OF_MONTH, daysLater);
        return new java.sql.Timestamp(tempCal.getTime().getTime());
    }
    public static java.sql.Timestamp getNextDayStart(java.sql.Timestamp stamp) {
        return getDayStart(stamp, 1);
    } 
    public static java.sql.Timestamp getDayEnd(java.sql.Timestamp stamp) {
        return getDayEnd(stamp, 0);
    }
    public static java.sql.Timestamp getDayEnd(java.sql.Timestamp stamp, int daysLater) {
        Calendar tempCal = Calendar.getInstance();
        tempCal.setTime(new java.util.Date(stamp.getTime()));
        tempCal.set(
            tempCal.get(Calendar.YEAR),
            tempCal.get(Calendar.MONTH),
            tempCal.get(Calendar.DAY_OF_MONTH),
            23,
            59,
            59);
        tempCal.add(Calendar.DAY_OF_MONTH, daysLater);
        return new java.sql.Timestamp(tempCal.getTime().getTime());
    }
    public static java.sql.Timestamp getWeekNo(Date d) {
        return null; 
    } 
    public static java.sql.Date toSqlDate(String date) {
        java.util.Date newDate = toDate(date, "00:00:00");
        if (newDate != null) {
            return new java.sql.Date(newDate.getTime());
        } else {
            return null;
        }
    } 
    public static java.sql.Date toSqlDateForDateTime(String dateTime) {
        java.util.Date newDate = toDate(dateTime);
        if (newDate != null) {
            return new java.sql.Date(newDate.getTime());
        } else {
            return null;
        }
    }
    public static java.sql.Date toSqlDate(String monthStr, String dayStr, String yearStr) {
        java.util.Date newDate = toDate(monthStr, dayStr, yearStr, "0", "0", "0");
        if (newDate != null) {
            return new java.sql.Date(newDate.getTime());
        } else {
            return null;
        }
    } 
    public static java.sql.Date toSqlDate(int month, int day, int year) {
        java.util.Date newDate = toDate(month, day, year, 0, 0, 0);
        if (newDate != null) {
            return new java.sql.Date(newDate.getTime());
        } else {
            return null;
        }
    }
    public static java.sql.Time toSqlTime(String time) {
        java.util.Date newDate = toDate("1970" + datePartition + "1" + datePartition + "1", time);
        if (newDate != null) {
            return new java.sql.Time(newDate.getTime());
        } else {
            return null;
        }
    }
    public static java.sql.Time toSqlTime(String hourStr, String minuteStr, String secondStr) {
        java.util.Date newDate = toDate("0", "0", "0", hourStr, minuteStr, secondStr);
        if (newDate != null) {
            return new java.sql.Time(newDate.getTime());
        } else {
            return null;
        }
    }
    public static java.sql.Time toSqlTime(int hour, int minute, int second) {
        java.util.Date newDate = toDate(0, 0, 0, hour, minute, second);
        if (newDate != null) {
            return new java.sql.Time(newDate.getTime());
        } else {
            return null;
        }
    } 
    public static java.sql.Timestamp toTimestamp(String dateTime) {
        java.util.Date newDate = toDate(dateTime);
        if (newDate != null) {
            return new java.sql.Timestamp(newDate.getTime());
        } else {
            return null;
        }
    } 
    public static java.sql.Timestamp toTimestamp(String date, String time) {
        if ((date == null) || (time == null)) {
            return null;
        }
        return toTimestamp(date + " " + time); 
    } 
    public static java.sql.Timestamp toTimestamp(
        String monthStr,
        String dayStr,
        String yearStr,
        String hourStr,
        String minuteStr,
        String secondStr) {
        java.util.Date newDate = toDate(monthStr, dayStr, yearStr, hourStr, minuteStr, secondStr);
        if (newDate != null) {
            return new java.sql.Timestamp(newDate.getTime());
        } else {
            return null;
        }
    } 
    public static java.sql.Timestamp toTimestamp(int month, int day, int year, int hour, int minute, int second) {
        java.util.Date newDate = toDate(month, day, year, hour, minute, second);
        if (newDate != null) {
            return new java.sql.Timestamp(newDate.getTime());
        } else {
            return null;
        }
    } 
    public static java.util.Date toDate(String dateTime) {
        try {
            return new Date(
                DateFormat
                    .getDateTimeInstance(DateFormat.MEDIUM, DateFormat.MEDIUM, Locale.CHINA)
                    .parse(dateTime)
                    .getTime());
        } catch (ParseException e) {
            e.printStackTrace();
            return null;
        }
    }
    public static java.util.Date toDate(String date, String time) {
        if ((date == null) || (time == null)) {
            return null;
        } 
        return toDate(date + " " + time); 
    }
    public static java.util.Date toDate(
        String monthStr,
        String dayStr,
        String yearStr,
        String hourStr,
        String minuteStr,
        String secondStr) {
        int month, day, year, hour, minute, second;
        try {
            month = Integer.parseInt(monthStr);
            day = Integer.parseInt(dayStr);
            year = Integer.parseInt(yearStr);
            hour = Integer.parseInt(hourStr);
            minute = Integer.parseInt(minuteStr);
            second = Integer.parseInt(secondStr);
        } catch (Exception e) {
            return null;
        }
        return toDate(month, day, year, hour, minute, second);
    }
    public static java.util.Date toDate(int month, int day, int year, int hour, int minute, int second) {
        Calendar calendar = Calendar.getInstance();
        try {
            calendar.set(year, month - 1, day, hour, minute, second);
        } catch (Exception e) {
            return null;
        }
        return new java.util.Date(calendar.getTime().getTime());
    }
    public static String toDateString(java.util.Date date) {
        if (date == null) {
            return "";
        }
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        int month = calendar.get(Calendar.MONTH) + 1;
        int day = calendar.get(Calendar.DAY_OF_MONTH);
        int year = calendar.get(Calendar.YEAR);
        String monthStr;
        String dayStr;
        String yearStr;
        if (month < 10) {
            monthStr = "0" + month;
        } else {
            monthStr = "" + month;
        }
        if (day < 10) {
            dayStr = "0" + day;
        } else {
            dayStr = "" + day;
        }
        yearStr = "" + year;
        return yearStr + "-" + monthStr + "-" + dayStr;
    } 
    public static String toTimeString(java.util.Date date) {
        if (date == null) {
            return "";
        }
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        return toTimeString(
            calendar.get(Calendar.HOUR_OF_DAY),
            calendar.get(Calendar.MINUTE),
            calendar.get(Calendar.SECOND));
    } 
    public static String toTimeString(int hour, int minute, int second) {
        String hourStr;
        String minuteStr;
        String secondStr;
        if (hour < 10) {
            hourStr = "0" + hour;
        } else {
            hourStr = "" + hour;
        }
        if (minute < 10) {
            minuteStr = "0" + minute;
        } else {
            minuteStr = "" + minute;
        }
        if (second < 10) {
            secondStr = "0" + second;
        } else {
            secondStr = "" + second;
        }
        if (second == 0) {
            return hourStr + ":" + minuteStr;
        } else {
            return hourStr + ":" + minuteStr + ":" + secondStr;
        }
    }
    public static String toDateTimeString(java.util.Date date) {
        if (date == null) {
            return "";
        }
        String dateString = toDateString(date);
        String timeString = toTimeString(date);
        if ((dateString != null) && (timeString != null)) {
            return dateString + " " + timeString;
        } else {
            return "";
        }
    }
    public static String toDateTimeString(java.sql.Date date) {
        if (date == null) {
            return "";
        }
        return toDateTimeString(new java.util.Date(date.getTime()));
    }
    public static java.sql.Timestamp prevMonthBegin(java.sql.Timestamp stamp) {
        Calendar mth = Calendar.getInstance(); 
        mth.setTime(new java.util.Date(stamp.getTime()));
        mth.add(Calendar.MONTH, -1);
        mth.set(Calendar.DAY_OF_MONTH, 1);
        mth.set(Calendar.HOUR_OF_DAY, 0);
        mth.set(Calendar.MINUTE, 0);
        mth.set(Calendar.SECOND, 0);
        mth.set(Calendar.AM_PM, Calendar.AM);
        return new java.sql.Timestamp(mth.getTime().getTime());
    } 
    public static java.sql.Timestamp prevMonthEnd(java.sql.Timestamp stamp) {
        Calendar mth = Calendar.getInstance();
        mth.setTime(new java.util.Date(stamp.getTime()));
        mth.add(Calendar.MONTH, -1);
        mth.set(Calendar.DAY_OF_MONTH, mth.getActualMaximum(Calendar.DAY_OF_MONTH));
        mth.set(Calendar.HOUR_OF_DAY, 23);
        mth.set(Calendar.MINUTE, 59);
        mth.set(Calendar.SECOND, 59);
        return new java.sql.Timestamp(mth.getTime().getTime());
    } 
    public static java.sql.Timestamp monthBegin() {
        Calendar mth = Calendar.getInstance();
        mth.set(Calendar.DAY_OF_MONTH, 1);
        mth.set(Calendar.HOUR_OF_DAY, 0);
        mth.set(Calendar.MINUTE, 0);
        mth.set(Calendar.SECOND, 0);
        mth.set(Calendar.AM_PM, Calendar.AM);
        return new java.sql.Timestamp(mth.getTime().getTime());
    } 
    public static java.sql.Timestamp monthEnd() {
        Calendar mth = Calendar.getInstance();
        mth.set(Calendar.DAY_OF_MONTH, mth.getActualMaximum(Calendar.DAY_OF_MONTH));
        mth.set(Calendar.HOUR_OF_DAY, 23);
        mth.set(Calendar.MINUTE, 59);
        mth.set(Calendar.SECOND, 59);
        return new java.sql.Timestamp(mth.getTime().getTime());
    } 
    public static String getMonth(String date) {
        if ((date == null) || date.equals("")) {
            return "";
        }
        int dateSlash1 = date.indexOf(UtilDateTime.datePartition);
        int dateSlash2 = date.lastIndexOf(UtilDateTime.datePartition);
        return date.substring(dateSlash1 + 1, dateSlash2);
    }
    public static String getQuarter(String date) {
        int month = Integer.parseInt(getMonth(date));
        if (month < 4) {
            return "1";
        } else if (month < 7) {
            return "2";
        } else if (month < 10) {
            return "3";
        } else if (month < 13) {
            return "4";
        } else {
            return null;
        }
    }
    public static int getQuarterInt(String date) {
        int month = Integer.parseInt(getMonth(date));
        if (month < 4) {
            return 1;
        } else if (month < 7) {
            return 2;
        } else if (month < 10) {
            return 3;
        } else if (month < 13) {
            return 4;
        } else {
            return 0;
        }
    }
    public static String getYear(String date) {
        if ((date == null) || date.equals("")) {
            return "";
        }
        int dateSlash1 = date.indexOf(UtilDateTime.datePartition);
        return date.substring(0, dateSlash1);
    }
    public static String getDay(String date) {
        if ((date == null) || date.equals("")) {
            return "";
        }
        int dateSlash2 = date.lastIndexOf(UtilDateTime.datePartition);
        return date.substring(dateSlash2 + 1, date.length() < dateSlash2 + 3 ? date.length() : dateSlash2 + 3);
    } 
    public static String getHour(String date) {
        if ((date == null) || date.equals("")) {
            return "";
        }
        int dateSlash1 = date.indexOf(":");
        return date.substring(dateSlash1 - 2 < 0 ? 0 : dateSlash1 - 2, dateSlash1).trim();
    }
    public static String getMinute(String date) {
        if ((date == null) || date.equals("")) {
            return "";
        }
        int dateSlash1 = date.indexOf(":");
        int dateSlash2 = date.lastIndexOf(":");
        if (dateSlash1 == dateSlash2) {
            return date
                .substring(dateSlash1 + 1, date.length() < dateSlash2 + 3 ? date.length() : dateSlash2 + 3)
                .trim();
        }
        return date.substring(dateSlash1 + 1, dateSlash2).trim();
    }
    public static String getSecond(String date) {
        if ((date == null) || date.equals("")) {
            return "";
        }
        int dateSlash1 = date.indexOf(":");
        int dateSlash2 = date.lastIndexOf(":");
        if (dateSlash1 == dateSlash2) {
            return "0";
        }
        return date.substring(dateSlash2 + 1, date.length() < dateSlash2 + 3 ? date.length() : dateSlash2 + 3).trim();
    }
    public static String getYear(java.sql.Date date) {
        String str = toDateTimeString(date);
        return getYear(str);
    }
    public static int getQuarterInt(java.sql.Date date) {
        String str = toDateTimeString(date);
        return getQuarterInt(str);
    }
    public static String getQuarter(java.sql.Date date) {
        String str = toDateTimeString(date);
        return getQuarter(str);
    } 
    public static String getYear(java.util.Date date) {
        String str = toDateTimeString(date);
        return getYear(str);
    } 
    public static String getMonth(java.sql.Date date) {
        String str = toDateTimeString(date);
        return getMonth(str);
    } 
    public static String getMonth(java.util.Date date) {
        String str = toDateTimeString(date);
        return getMonth(str);
    } 
    public static int getQuarterInt(java.util.Date date) {
        String str = toDateTimeString(date);
        return getQuarterInt(str);
    } 
    public static String getQuarter(java.util.Date date) {
        String str = toDateTimeString(date);
        return getQuarter(str);
    } 
    public static String getQuarter(String year, String month, boolean isNow) {
        if ((year == null) || (month == null) || "".equals(year) || "".equals(month)) {
            if (isNow) {
                return getQuarter(nowDate());
            } else {
                return null;
            }
        }
        String str =
            new StringBuffer(year)
                .append(UtilDateTime.datePartition)
                .append(month)
                .append(UtilDateTime.datePartition)
                .append("01")
                .toString();
        return getQuarter(str);
    }
    public static int getQuarterInt(String year, String month, boolean isNow) {
        if ((year == null) || (month == null) || "".equals(year) || "".equals(month)) {
            if (isNow) {
                return getQuarterInt(nowDate());
            } else {
                return 0;
            }
        }
        String str =
            new StringBuffer(year)
                .append(UtilDateTime.datePartition)
                .append(month)
                .append(UtilDateTime.datePartition)
                .append("01")
                .toString();
        return getQuarterInt(str);
    }
    public static String getDay(java.sql.Date date) {
        String str = toDateTimeString(date);
        return getDay(str);
    }
    public static String getDay(java.util.Date date) {
        String str = toDateTimeString(date);
        return getDay(str);
    }
    public static String getHour(java.sql.Date date) {
        String str = toDateTimeString(date);
        return getHour(str);
    } 
    public static String getHour(java.util.Date date) {
        String str = toDateTimeString(date);
        return getHour(str);
    }
    public static String getMinute(java.sql.Date date) {
        String str = toDateTimeString(date);
        return getMinute(str);
    }
    public static String getMinute(java.util.Date date) {
        String str = toDateTimeString(date);
        return getMinute(str);
    } 
    public static String getSecond(java.sql.Date date) {
        String str = toDateTimeString(date);
        return getSecond(str);
    } 
    public static String getSecond(java.util.Date date) {
        String str = toDateTimeString(date);
        return getSecond(str);
    } 
    public static String getWeek(java.util.Date date) {
        if (date == null) {
            return "";
        }
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        int weekNum = calendar.get(Calendar.DAY_OF_WEEK) - 1;
        if (weekNum == 0) {
            weekNum = 7;
        }
        return String.valueOf(weekNum);
    }
    public static String getWeek(java.sql.Date date) {
        if (date == null) {
            return "";
        }
        return getWeek(new java.util.Date(date.getTime()));
    }
    public static String getWeek(String date) {
        return getWeek(toSqlDate(date));
    } 
    public static String getWeek() {
        Calendar calendar = Calendar.getInstance();
        int weekNum = calendar.get(Calendar.DAY_OF_WEEK) - 1;
        if (weekNum == 0) {
            weekNum = 7;
        }
        return String.valueOf(weekNum);
    }
    public static java.sql.Timestamp getTimestamp(long time) {
        return new java.sql.Timestamp(time);
    }
    public static String getTimeStr(long time) {
        return new java.sql.Timestamp(time).toString();
    }
    public static String getNowTime() {
        java.util.Date date = new java.util.Date();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSS");
        String xzsj = sdf.format(date);
        return xzsj;
    }
    public static long getExpendTime(String startTime, String endTime) {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSS");
        java.util.Date start;
        java.util.Date end;
        long time = 0;
        try {
            start = sdf.parse(startTime);
            end = sdf.parse(endTime);
            time = start.getTime() - end.getTime();
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return time;
    }
    public static void countEnd(String desc, long startTime) {
        long time = new Date().getTime(); 
        long sec = (time - startTime) / (1000 * 60);
        long miao = (time - startTime) % (1000 * 60) / 1000;
        long haomiao = (time - startTime) % (1000 * 60) % 1000;
        log.info(desc+sec+"-"+miao+"-"+haomiao);
    } 
    public static long countStart(String desc) {
        StringBuffer endS = new StringBuffer();
        long time = new Date().getTime();
        log.info(endS.append(desc).append("[").append(new Timestamp(time)).append("]").toString());
        return time; 
    } 
    public static java.sql.Timestamp getWestWeekStart(java.sql.Timestamp stamp) {
        Calendar tempCal = Calendar.getInstance();
        tempCal.setTime(new java.util.Date(stamp.getTime()));
        tempCal.set(Calendar.DAY_OF_WEEK, tempCal.getActualMinimum(Calendar.DAY_OF_WEEK));
        tempCal.set(Calendar.HOUR_OF_DAY, 0);
        tempCal.set(Calendar.MINUTE, 0);
        tempCal.set(Calendar.SECOND, 0);
        return new java.sql.Timestamp(tempCal.getTime().getTime());
    }
    public static java.sql.Timestamp getWestWeekEnd(java.sql.Timestamp stamp) {
        Calendar tempCal = Calendar.getInstance();
        tempCal.setTime(new java.util.Date(stamp.getTime()));
        tempCal.set(Calendar.DAY_OF_WEEK, tempCal.getActualMaximum(Calendar.DAY_OF_WEEK));
        tempCal.set(Calendar.HOUR_OF_DAY, 23);
        tempCal.set(Calendar.MINUTE, 59);
        tempCal.set(Calendar.SECOND, 59);
        return new java.sql.Timestamp(tempCal.getTime().getTime());
    }
    public static java.sql.Timestamp getWeekStart(java.sql.Timestamp stamp) {
        if ("7".equals(getWeek(stamp.toString()))) {
            return getNextDayStart(getWestWeekStart(getDayStart(stamp, -1)));
        } else {
            return getNextDayStart(getWestWeekStart(stamp));
        }
    } 
    public static java.sql.Timestamp getWeekEnd(java.sql.Timestamp stamp) {
        if ("7".equals(getWeek(stamp.toString()))) {
            return getDayEnd(getNextDayStart(getWestWeekEnd(getDayStart(stamp, -1))));
        } else {
            return getDayEnd(getNextDayStart(getWestWeekEnd(stamp)));
        }
    }
    public static java.sql.Timestamp getMonthStart(java.sql.Timestamp stamp) {
        Calendar tempCal = Calendar.getInstance();
        tempCal.setTime(new java.util.Date(stamp.getTime()));
        tempCal.set(Calendar.DAY_OF_MONTH, tempCal.getActualMinimum(Calendar.DAY_OF_MONTH));
        tempCal.set(Calendar.HOUR_OF_DAY, 0);
        tempCal.set(Calendar.MINUTE, 0);
        tempCal.set(Calendar.SECOND, 0);
        return new java.sql.Timestamp(tempCal.getTime().getTime());
    } 
    public static java.sql.Timestamp getMonthEnd(java.sql.Timestamp stamp) {
        Calendar tempCal = Calendar.getInstance();
        tempCal.setTime(new java.util.Date(stamp.getTime()));
        tempCal.set(Calendar.DAY_OF_MONTH, tempCal.getActualMaximum(Calendar.DAY_OF_MONTH));
        tempCal.set(Calendar.HOUR_OF_DAY, 23);
        tempCal.set(Calendar.MINUTE, 59);
        tempCal.set(Calendar.SECOND, 59);
        return new java.sql.Timestamp(tempCal.getTime().getTime());
    }
    public static java.sql.Timestamp getYearStart(java.sql.Timestamp stamp) {
        Calendar tempCal = Calendar.getInstance();
        tempCal.setTime(new java.util.Date(stamp.getTime()));
        tempCal.set(Calendar.DAY_OF_YEAR, tempCal.getActualMinimum(Calendar.DAY_OF_YEAR));
        tempCal.set(Calendar.HOUR_OF_DAY, 0);
        tempCal.set(Calendar.MINUTE, 0);
        tempCal.set(Calendar.SECOND, 0);
        return new java.sql.Timestamp(tempCal.getTime().getTime());
    }
    public static java.sql.Timestamp getYearEnd(java.sql.Timestamp stamp) {
        Calendar tempCal = Calendar.getInstance();
        tempCal.setTime(new java.util.Date(stamp.getTime()));
        tempCal.set(Calendar.DAY_OF_YEAR, tempCal.getActualMaximum(Calendar.DAY_OF_YEAR));
        tempCal.set(Calendar.HOUR_OF_DAY, 23);
        tempCal.set(Calendar.MINUTE, 59);
        tempCal.set(Calendar.SECOND, 59);
        return new java.sql.Timestamp(tempCal.getTime().getTime());
    } 
    public static int getWeekOfMonth(Date d) {
        Calendar calendar = Calendar.getInstance();
        if (d == null) {
            d = new Date();
        }
        calendar.setTime(d);
        int weekNum = calendar.get(Calendar.DAY_OF_WEEK) - 1;
        if (weekNum == 0) {
            weekNum = 7;
        }
        int v = Integer.parseInt(getDay(d)) - weekNum;
        if (v < 0) {
            return 1;
        } else {
            return (((Integer.parseInt(getDay(d)) - weekNum) + 1) / 7) + 2;
        }
    } 
    public static int getWeekOfMonthFormDate(String dateString) {
        return getWeekOfMonth(toDate(dateString + " 00:00:01"));
    } 
    public static int getWeekOfMonthFormDateTime(String dateTimeString) {
        return getWeekOfMonth(toDate(dateTimeString));
    } 
    public static int getWeekOfMonth() {
        return getWeekOfMonth(null);
    }
    public static void printfWeeks(String date) throws Exception {
        DateFormat dateFormat = new SimpleDateFormat("yyyy-MM");
        Date date1 = dateFormat.parse(date);
        Calendar calendar = new GregorianCalendar();
        calendar.setTime(date1);
        int days = calendar.getActualMaximum(Calendar.DAY_OF_MONTH);
        System.out.println("days:" + days);
        int count = 0;
        for (int i = 1; i <= days; i++) {
            DateFormat dateFormat1 = new SimpleDateFormat("yyyy-MM-dd");
            Date date2 = dateFormat1.parse(date + "-" + i);
            calendar.clear();
            calendar.setTime(date2);
            int k = new Integer(calendar.get(Calendar.DAY_OF_WEEK));
            if (k == 1) {// 若当天是周日
                count++;
                System.out.println("-----------------------------------");
                System.out.println("第" + count + "周");
                if (i - 6 <= 1) {
                    System.out.println("本周开始日期:" + date + "-" + 1);
                } else {
                    System.out.println("本周开始日期:" + date + "-" + (i - 6));
                }
                System.out.println("本周结束日期:" + date + "-" + i);
                System.out.println("-----------------------------------");
            }
            if (k != 1 && i == days) {// 若是本月最好一天，且不是周日
                count++;
                System.out.println("-----------------------------------");
                System.out.println("第" + count + "周");
                System.out.println("本周开始日期:" + date + "-" + (i - k + 2));
                System.out.println("本周结束日期:" + date + "-" + i);
                System.out.println("-----------------------------------");
            }
        }
    }
    public static void main(String[] args) throws Exception {
        long team_start = 1450009168579l;
        long team_end = team_start + 1*21*60*60*1000;
        long now = System.currentTimeMillis();
        System.out.println(getTimeStr(team_start));
        System.out.println(getTimeStr(team_end));
        System.out.println(getTimeStr(now));
        System.out.println(now<team_end);  
        printfWeeks("2017-09");
    }
}
